package cn.edw.swu.bc.swucommunity

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.*
import androidx.core.app.ActivityCompat
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class MainActivity : AppCompatActivity() {
    lateinit var adapter: MainLayoutAdapter
    val REQ_CODE = 1024
    val REQ_CODE_CONTACT = 1025

    var toCallPhone=null
    val data = arrayListOf<String>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initData()
    }

    fun initData() {
        data.add("测试1")
        data.add("测试2")
        data.add("测试3")
        data.add("测试4")
        adapter.setData(data)
    }

    fun initView() {
        val viewpager = findViewById<ViewPager2>(R.id.viewpager)
        val tabLayout = findViewById<TabLayout>(R.id.navigation_bar)

        adapter = MainLayoutAdapter(this)
        viewpager.adapter = adapter

        TabLayoutMediator(tabLayout, viewpager) { tab, pos ->
            val item = adapter.getItem(pos)
            tab.text = item
            val drawable= getDrawable(R.drawable.ic_myself)
            tab.icon=drawable
        }.attach()
    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if(requestCode == REQ_CODE){
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                toCallPhone?.let{
                    SessionFragment.doCall(it,this)
                }
            }else{
                Toast.makeText(this,"您必须授权才能拨打电话", Toast.LENGTH_LONG).show()
            }
        }else if(requestCode == REQ_CODE_CONTACT){
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                SessionFragment.readContacts(this)
            }else{
                Toast.makeText(this,"您必须授权才能读取联系人", Toast.LENGTH_LONG).show()
            }
        }
    }




}